import java.util.*;
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        //如果head为空并且k<=0不合法，那么返回null
        if(head == null || k <= 0) {
            return null;
        }

        //定义快慢指针
        ListNode fast = head;
        ListNode slow = head;
        while(k - 1 != 0) {//倒数第k个，fast走 k-1 步
            fast = fast.next;
            //如果此时fast已经是null,说明k超过结点个数,返回null
            if(fast == null) {
                return null;
            }
            k--;
        }

        //然后fast和slow同时走
        //直到fast走到最后一个结点，slow的位置就是倒数第k个结点
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}